package bb.lanxing.model.database;

import android.database.sqlite.SQLiteDatabase;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
import com.alibaba.fastjson.annotation.JSONField;
import com.baidu.mapapi.model.LatLng;
//import com.orm.SugarRecord;
import com.orm.SugarTransactionHelper;
//import com.orm.dsl.Table;
//import com.orm.query.Select;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

import java.util.List;

public class TrackPointOther extends LitePalTableBase implements Parcelable, ITrackPoint {
    public static final Parcelable.Creator<TrackPointOther> CREATOR = new Parcelable.Creator<TrackPointOther>() {

        public TrackPointOther createFromParcel(Parcel parcel) {
            return new TrackPointOther(parcel);
        }


        public TrackPointOther[] newArray(int i) {
            return new TrackPointOther[i];
        }
    };
    private static final String TAG = "TrackPointOther";
    private static SQLiteDatabase db;
    private double accuracy;
    private double altitude;
    private double bearing;
    private int cadence;
    private double distance;
    private long duration;
    private double elevation;
    private int flag;
    private double grade;
    private int heartrate;
    private double latitude;
    private double longitude;
    private double power;
    private double pressure;
    private int source;
    private double speed;
    private int sumCadence;
    private int sumWheel;
    private double temperature;
    private long time;
    @JSONField(name = "wheelrpm")
    private int wheelRpm;
    @JSONField(name = "workoutid")
    private long workoutId;



    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public int getPointType() {
        return 0;
    }

    public static long getCountByWorkout(long j) {

        String wid = String.valueOf(j);
        List<TrackPointOther> l = LitePal.where("workoutId=?", wid).find(TrackPointOther.class);

        long count = l.size();

        return count;

//        return Select.from(TrackPointOther.class).where("workout_id = ? ", new String[]{String.valueOf(j)}).count();

    }

    public static void savePoints(final List<TrackPointOther> list) {
        Log.d(TAG, "savePoints: " + list);

        try{
            LitePal.beginTransaction();

            for (TrackPointOther trackPointOther : list) {
                trackPointOther.save();
            }

            LitePal.setTransactionSuccessful();
        }
        finally {
            LitePal.endTransaction();
        }

//        SugarTransactionHelper.doInTansaction(new SugarTransactionHelper.Callback() {
//            @Override
//            public void manipulateInTransaction() {
//                for (TrackPointOther trackPointOther : list) {
//                    trackPointOther.save();
//                }
//            }
//        });



    }

    @Override
    public double getAccuracy() {
        return this.accuracy;
    }

    public void setAccuracy(double d) {
        this.accuracy = d;
    }

    @Override
    public double getAltitude() {
        return this.altitude;
    }

    public void setAltitude(double d) {
        this.altitude = d;
    }

    @Override
    public double getLatitude() {
        return this.latitude;
    }

    public void setLatitude(double d) {
        this.latitude = d;
    }

    @Override
    public double getLongitude() {
        return this.longitude;
    }

    public void setLongitude(double d) {
        this.longitude = d;
    }

    @Override
    public double getBearing() {
        return this.bearing;
    }

    public void setBearing(double d) {
        this.bearing = d;
    }

    @Override
    public double getSpeed() {
        return this.speed;
    }

    public void setSpeed(double d) {
        this.speed = d;
    }

    @Override
    public long getTime() {
        return this.time;
    }

    public void setTime(long j) {
        this.time = j;
    }

    @Override
    public long getWorkoutId() {
        return this.workoutId;
    }

    public void setWorkoutId(long j) {
        this.workoutId = j;
    }

    public static SQLiteDatabase getDb() {
        return db;
    }

    public static void setDb(SQLiteDatabase sQLiteDatabase) {
        db = sQLiteDatabase;
    }

    @Override
    public int getCadence() {
        return this.cadence;
    }

    public void setCadence(int i) {
        this.cadence = i;
    }

    @Override
    public int getHeartrate() {
        return this.heartrate;
    }

    @Override
    public void setHeartrate(int i) {
        this.heartrate = i;
    }

    @Override
    public int getWheelRpm() {
        return this.wheelRpm;
    }

    public void setWheelRpm(int i) {
        this.wheelRpm = i;
    }

    @Override
    public int getSumCadence() {
        return this.sumCadence;
    }

    public void setSumCadence(int i) {
        this.sumCadence = i;
    }

    @Override
    public int getSumWheel() {
        return this.sumWheel;
    }

    public void setSumWheel(int i) {
        this.sumWheel = i;
    }

    @Override
    public int getSource() {
        return this.source;
    }

    public void setSource(int i) {
        this.source = i;
    }

    @Override
    public int getFlag() {
        return this.flag;
    }

    public void setFlag(int i) {
        this.flag = i;
    }

    @Override
    public double getDistance() {
        return this.distance;
    }

    public void setDistance(double d) {
        this.distance = d;
    }

    @Override
    public long getDuration() {
        return this.duration;
    }

    public void setDuration(long j) {
        this.duration = j;
    }

    @Override
    public double getPressure() {
        return this.pressure;
    }

    public void setPressure(double d) {
        this.pressure = d;
    }

    @Override
    public double getTemperature() {
        return this.temperature;
    }

    public void setTemperature(double d) {
        this.temperature = d;
    }

    @Override
    public double getElevation() {
        return this.elevation;
    }

    public void setElevation(double d) {
        this.elevation = d;
    }

    @Override
    public double getGrade() {
        return this.grade;
    }

    public void setGrade(double d) {
        this.grade = d;
    }

    @Override
    public LatLng getLatLng() {
        return new LatLng(this.latitude, this.longitude);
    }

    @Override
    public double getPower() {
        return this.power;
    }

    @Override
    public void setPower(double d) {
        this.power = d;
    }

    @Override
    public double getAltitudeGps() {
        return this.altitude;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeDouble(this.accuracy);
        parcel.writeDouble(this.altitude);
        parcel.writeDouble(this.latitude);
        parcel.writeDouble(this.longitude);
        parcel.writeDouble(this.bearing);
        parcel.writeDouble(this.speed);
        parcel.writeLong(this.time);
        parcel.writeLong(this.workoutId);
        parcel.writeInt(this.cadence);
        parcel.writeInt(this.heartrate);
        parcel.writeInt(this.wheelRpm);
        parcel.writeInt(this.sumCadence);
        parcel.writeInt(this.sumWheel);
        parcel.writeInt(this.source);
        parcel.writeInt(this.flag);
        parcel.writeDouble(this.distance);
        parcel.writeLong(this.duration);
        parcel.writeDouble(this.pressure);
        parcel.writeDouble(this.temperature);
        parcel.writeDouble(this.elevation);
        parcel.writeDouble(this.grade);
        parcel.writeDouble(this.power);
    }

    public TrackPointOther() {
        this.source = 1;
    }

    protected TrackPointOther(Parcel parcel) {
        this.source = 1;
        this.accuracy = parcel.readDouble();
        this.altitude = parcel.readDouble();
        this.latitude = parcel.readDouble();
        this.longitude = parcel.readDouble();
        this.bearing = parcel.readDouble();
        this.speed = parcel.readDouble();
        this.time = parcel.readLong();
        this.workoutId = parcel.readLong();
        this.cadence = parcel.readInt();
        this.heartrate = parcel.readInt();
        this.wheelRpm = parcel.readInt();
        this.sumCadence = parcel.readInt();
        this.sumWheel = parcel.readInt();
        this.source = parcel.readInt();
        this.flag = parcel.readInt();
        this.distance = parcel.readDouble();
        this.duration = parcel.readLong();
        this.pressure = parcel.readDouble();
        this.temperature = parcel.readDouble();
        this.elevation = parcel.readDouble();
        this.grade = parcel.readDouble();
        this.power = parcel.readDouble();
    }
}
